package kr.hkit.powerjava.chapter16;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.EventHandler;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.BoxLayout;
import javax.swing.JButton;

class MyListener01 implements ActionListener {// 외부클래스

	public void prnTest(){
		System.out.println("gggg");
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		JButton btn = (JButton) e.getSource();
		btn.setText("외부클래스를 이용한 핸들러 리스너");
	}

}

public class EventHandlerEx extends JFrame implements ActionListener {// im -
																		// 클래스가
																		// 처리를
																		// 위함

	private JPanel contentPane;
	private JButton btnHandler01;
	private JButton btnHandler02;
	private JButton btnHandler03;
	private JButton btnHandler04;
	private JButton btnHandler05;

	class MyListener02 implements ActionListener {// 내부 클래스

		@Override
		public void actionPerformed(ActionEvent e) {
			if (e.getSource() == btnHandler02) {
				btnHandler02.setText("내부 클래스를 이용한 핸들러 리스너");
			}
		}
	}

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					EventHandlerEx frame = new EventHandlerEx();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public EventHandlerEx() {
		initialize();
	}

	private void initialize() {
		setTitle("이벤트 핸들러");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));

		MyListener01 listener01 = new MyListener01();
		btnHandler01 = new JButton("외부클래스");
		btnHandler01.addActionListener(listener01);
		contentPane.add(btnHandler01);

		btnHandler02 = new JButton("내부클래스");
		MyListener02 listener02 = new MyListener02();
		btnHandler02.addActionListener(listener02);
		contentPane.add(btnHandler02);

		btnHandler03 = new JButton("클래스처리");
		btnHandler03.addActionListener(this);
		contentPane.add(btnHandler03);

		btnHandler04 = new JButton("익명클래스");
		btnHandler04.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				if (e.getSource() == btnHandler04) {
					btnHandler04.setText("익명 클래스를 이용한 핸들러 리스너");
				}
			}
		});
		contentPane.add(btnHandler04);
		
		btnHandler05 = new JButton("EventHandler Class");
//		btnHandler05.addActionListener(EventHandler.create(ActionListener.class, this, "setBtn05Text"));
//		btnHandler05.addActionListener(EventHandler.create(ActionListener.class, listener01, "actionPerformed"));
		
		//this -> JFrame Jframe에 있는 setBtn05Text 메소드를 수행
		
		contentPane.add(btnHandler05);
	}

	public void setBtn05Text(){
		btnHandler05.setText("EventHandler 클래스를 이용");
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {// 클래스가 처리
		if (e.getSource() == btnHandler03) {
			btnHandler03.setText("클래스를 이용한 핸들러 리스너");
		}
	}

}
